﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using BOSS.Domain.Security;

namespace BOSS.Web
{
    /// <summary>
    /// HTTP 请求 URL 辅助类
    /// </summary>
    public sealed class RequestUrlHelper
    {
#if DEBUG
        private static readonly Regex hostRegex = new Regex(@"(((.+)\.)*(?<Domain>.+)\.)?erp\.chinalanfu\.com", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
#else
        private static readonly Regex hostRegex = new Regex(@"(((.+)\.)*(?<Domain>.+)\.)?erp\.chinalanfu\.com", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
#endif

        /// <summary>
        /// 获取主机地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetHost(HttpRequest request)
        {
            return request.Headers["Host"];
        }

        /// <summary>
        /// 获取主机地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetHost(HttpRequestBase request)
        {
            return request.Headers["Host"];
        }

        /// <summary>
        /// 获取主机端口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static int GetPort(HttpRequest request)
        {
            return request.Url.Port;
        }

        /// <summary>
        /// 获取主机端口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static int GetPort(HttpRequestBase request)
        {
            return request.Url.Port;
        }

        /// <summary>
        /// 获取所在域
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetDomain(HttpRequest request)
        {
            return GetDomain(GetHost(request));
        }

        /// <summary>
        /// 获取所在域
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetDomain(HttpRequestBase request)
        {
            return GetDomain(GetHost(request));
        }

        /// <summary>
        /// 获取所在域
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        public static string GetDomain(string host)
        {
            Match match = hostRegex.Match(host);
            string domain = match.Success ? match.Groups["Domain"].Value : null;
            if (string.IsNullOrEmpty(domain) || domain.Equals("www", StringComparison.OrdinalIgnoreCase))
            {
                return null;
            }
            return domain;
        }

        /// <summary>
        /// 构建首页的 URL
        /// </summary>
        /// <returns></returns>
        public static string BuildHomepageUrl()
        {
            return BuildHomepageUrl(null);
        }

        /// <summary>
        /// 构建首页的 URL
        /// </summary>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public static string BuildHomepageUrl(string relativeUrl)
        {
#if DEBUG
            string rootUrl = "http://erp.chinalanfu.com:20000";
#else
            string rootUrl = "http://erp.chinalanfu.com";
#endif
            if (string.IsNullOrEmpty(relativeUrl))
            {
                return "/default.aspx"; //rootUrl;
            }
            else if (relativeUrl.StartsWith("/"))
            {
                return string.Concat(rootUrl, relativeUrl);
            }
            else
            {
                return string.Concat(rootUrl, "/", relativeUrl);
            }
        }

        /// <summary>
        /// 构建公司门户的 URL
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static string BuildPortalUrl(string domain)
        {
            return BuildPortalUrl(domain, null);
        }

        /// <summary>
        /// 构建公司门户的 URL
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public static string BuildPortalUrl(string domain, string relativeUrl)
        {
#if DEBUG
            string rootUrl = string.Format("http://{0}.erp.chinalanfu.com:20000", domain.ToLower());
#else
            string rootUrl = string.Format("http://{0}.erp.chinalanfu.com", domain.ToLower());
#endif
            if (string.IsNullOrEmpty(relativeUrl))
            {
                return rootUrl;
            }
            else if (relativeUrl.StartsWith("/"))
            {
                return string.Concat(rootUrl, relativeUrl);
            }
            else
            {
                return string.Concat(rootUrl, "/", relativeUrl);
            }
        }

        /// <summary>
        /// 构建指定用户的首页
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public static string BuildHomeUrl(Account account)
        {
            if (account == null)
            {
                return RequestUrlHelper.BuildHomepageUrl("/Account/Login");
            }
            else
            {
                return RequestUrlHelper.BuildHomepageUrl();
            }
        }

        /// <summary>
        /// 构建指定用户的首页
        /// </summary>
        /// <param name="account"></param>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public static string BuildHomeUrl(Account account, string relativeUrl)
        {
            //if (account == null)
            //{
            //    return RequestUrlHelper.BuildHomepageUrl(relativeUrl);
            //}
            //else if (account.Roles.Contains(PredefinedRoles.SYSTEM_ADMIN)
            //    || account.Roles.Contains(PredefinedRoles.REPORT_COMMENTER))
            //{
            //    return RequestUrlHelper.BuildHomepageUrl(relativeUrl);
            //}
            //else
            //{
            //    return RequestUrlHelper.BuildPortalUrl(account.Domain, relativeUrl);
            //}

            return BuildHomeUrl(account);
        }
    }
}
